package com.guaoran.source.mybatis.demo.contorller;

import com.guaoran.source.mybatis.demo.bean.Demo;
import com.guaoran.source.mybatis.demo.dao.DemoMapper;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;

/**
 * @author : 孤傲然
 * @Description :
 * @date :2018/5/24 21:41
 */
public class DemoController {
    private final static String XML_CONFIG_PATH =
            "F:\\myProject\\guaoran\\java-commons\\spring-mybatis-show-source-project\\mybatis-source\\";
    public static void main(String[] args) {
        try {
            //1.获得SqlSession
            SqlSession sqlSession = getSqlSession();
            DemoMapper mapper = sqlSession.getMapper(DemoMapper.class);
            Demo demo = mapper.queryById(1L);
            System.out.println(demo);
            Demo demo2 = mapper.queryById(2L);
            System.out.println(demo2);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public static SqlSession getSqlSession() throws FileNotFoundException {
            InputStream configFile =
                new FileInputStream(XML_CONFIG_PATH +
                        "mybatis-show-source\\src\\main\\java\\com\\guaoran\\source\\mybatis\\demo\\mybatis-config.xml");
            SqlSessionFactory sqlSessionFactory =  new SqlSessionFactoryBuilder().build(configFile);
        return sqlSessionFactory.openSession();
    }
}
